/**
 * Created by 66xiaoqu on 2015/12/30.
 */
'use strict';

angular.module('mis.apiManagerController.controllers', [])
    .controller('apiCtrl', ['$scope', '$http', '$location', function ($scope, $http, $location) {
        $scope.currentPage = 1;
        $scope.pageSize = 15;
        var larSelect = new gObj.larSelect();
        $scope.category = "today";
        $scope.apiList = {};
        $scope.apiCodeCategoryDetail = function (api) {
            var body = {};
            switch ($scope.category) {
                case "rankTime":
                {
                    if ($scope.timeStart_) body.timeStart = new Date($scope.timeStart_).getTime();
                    if ($scope.timeEnd_) body.timeEnd = new Date($scope.timeEnd_).getTime();
                }
                default:
                {
                    body.code = api.service_code;
                    if ($scope.category) body.category = $scope.category;
                    else {
                        body.category = "today"

                    }
                }
                    break;
            }
            $scope.currentPage_detail = 1
            console.log(body);
            $scope.pagination_detail = new pagination({
                currentPage: $scope.currentPage_detail, pageSize: $scope.pageSize,
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/api/apiCodeList/:page/:size',
                ajax: $http,
                success: function (data) {
                    $('#loading').modal("close");
                    $scope.apiCodeList = data.data.list;
                    $scope.pageNumber_detail = data.data.totalPage;
                    $scope.count_detail = data.data.count;

                    $scope.currentPage_detail = $(".am-active").text();
                    $scope.pagination_detail.totalPage = data.data.totalPage;
                    $scope.pagination_detail.pageHeader($scope.pageNumber_detail);
                    $scope.pagination_detail.searchParams = body;
                    $('#count_detail').text("共"+$scope.pageNumber_detail+"页 有"+$scope.count_detail+"数据");
                },
                error: function (err) {
                    $scope.apiCodeList = null;
                    $('#loading').modal("close");
                },
                params: body
            },'_detail');
        }

        //分页
        $scope.pagination = new pagination({
            currentPage: $scope.currentPage, pageSize: $scope.pageSize,
            url: 'http://' + backOfficeHost + '/rest/v1.0/mis/api/apiList/:page/:size',
            ajax: $http,
            success: function (data) {
                $('#loading').modal("close");
                $scope.apiList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function (err) {
                $scope.apiList = null;
                $('#loading').modal("close");
            },
            params: {}
        });

        $scope.searchCategory = function () {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {};
            if ($scope.category) body.category = $scope.category;

            if(body.category =='rankTime') {

                //if ($scope.timeStart_) {
                //    body.timeStart = new Date($scope.timeStart_).getTime();
                //}
                //else {
                //
                //}
                //if ($scope.timeEnd_) body.timeEnd = new Date($scope.timeEnd_).getTime();
                if ($("#startDate").val()) body.timeStart = new Date($scope.timeStart_).getTime();
                if ($("#endDate").val()) body.timeEnd = new Date($scope.timeEnd_).getTime();

                if (body.timeStart && body.timeEnd && body.timeStart > body.timeEnd) {
                    alert('开始日期不能大于结束时间');
                    return;
                };

                if (!$("#startDate").val()) {
                    alert('请选择开始时间');
                    return;
                };

            };

            $('#loading').modal({target: '#loading'});

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/api/apiList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $('#loading').modal("close");
                $scope.apiList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function () {
                $scope.logList = null;
                $('#loading').modal("close");
                alert("输入错误 | 选择错误");
            });


        };

        //$scope.searchTime = function () {
        //    $scope.pagination.currentPage = 1;
        //    $scope.currentPage = 1;
        //
        //    var body = {};
        //    if ($scope.timeStart_) body.timeStart = new Date($scope.timeStart_).getTime();
        //    if ($scope.timeEnd_) body.timeEnd = new Date($scope.timeEnd_).getTime();
        //    body.category = $scope.category;
        //    console.log(body);
        //    $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/api/apiRankList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
        //        headers: {'Content-Type': 'application/json;charset=utf-8'}
        //    }).success(function (data) {
        //        $('#loading').modal("close");
        //        try{
        //        $scope.apiList = data.data.list;
        //        $scope.pageNumber = data.data.totalPage;
        //        $scope.count = data.data.count;
        //
        //        $scope.pagination.totalPage = data.data.totalPage;
        //        $scope.pagination.pageHeader($scope.pageNumber);
        //        $scope.pagination.searchParams = body;
        //        }
        //        catch (exception)
        //        { alert("至少输入起始日期,默认结束日期为到现在");}
        //    }).error(function () {
        //        $scope.apiList = null;
        //        $('#loading').modal("close");
        //    });
        //}
    }]);